﻿/*-----------------------------------------------
// File: PVPBattleBalance.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle 
{
    public class BattleBalance : BalanceBase 
    {
        public UILabel addCoinLabel;
        public UILabel addExpLabel;
        public UILabel addPrestigeLabel;
        public BalanceExpBar expBar;

        public UILabel attackerNameLabel;
        public UILabel defenderNameLabel;
        public UI.TextureKit attackerHeadIcon;
        public UI.TextureKit defenderHeadIcon;

        public UILabel loseAttackerNameLabel;
        public UILabel loseDefenderNameLabel;
        public UI.TextureKit loseAttackerHeadIcon;
        public UI.TextureKit loseDefenderHeadIcon;

        public UILabel rankFromLabel;
        public UILabel rankToLabel;
        public GameObject rankChangeObj;
        public GameObject rankChangeNoneObj;
        public List<DrawButton> drawButtons;

        public UILabel tipLabel;

        protected override void Refresh()
        {
            int preLevel = StaticData.playerData.level;
            int preExp = (int)StaticData.playerData.exp;
            base.Refresh();

            ProtoBuf.Message.ResultObject resultObject = null;
            long pvpRank = 0;

            if (GuideManager.Instance!=null&&GuideManager.Instance.GetRunningGuide().Equals("guide_pvp"))
            {
                ProtoBuf.Message.SCPvpFightResultGuide msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResultGuide;
                resultObject = msg.result;
                pvpRank = msg.myrank.pvpRank;
            }
            else
            {
                ProtoBuf.Message.SCPvpFightResult msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResult;
                resultObject = msg.result;
                pvpRank = msg.myrank.pvpRank;
            }


            expBar.Init(preExp, preLevel, GetAwardThingNum(resultObject, Dict.Config.TableConst.exp), (int)StaticData.playerData.exp, StaticData.playerData.level);
            addCoinLabel.GetComponent<TweenNumText>().Init(0, GetAwardThingNum(resultObject, Dict.Config.TableConst.coin));
            addExpLabel.GetComponent<TweenNumText>().Init(0, GetAwardThingNum(resultObject, Dict.Config.TableConst.exp));
            addPrestigeLabel.GetComponent<TweenNumText>().Init(0, GetAwardThingNum(resultObject, Dict.Config.TableConst.popularity));

            var dh0 = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.lastAttackerRankInfo.styleId);
            var dh1 = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.lastDefenderRankInfo.styleId);
            if (result == BalanceResult.Win)
            {
                expBar.Play();
                addCoinLabel.GetComponent<TweenNumText>().Play();
                addExpLabel.GetComponent<TweenNumText>().Play();
                addPrestigeLabel.GetComponent<TweenNumText>().Play();

                attackerHeadIcon.textureName = dh0.iconName;
                defenderHeadIcon.textureName = dh1.iconName;
                defenderHeadIcon.texture.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                attackerNameLabel.text = StaticData.lastAttackerRankInfo.nickName;
                defenderNameLabel.text = "[2f2f2f]" + StaticData.lastDefenderRankInfo.nickName + "[-]";

                foreach (var drawBtn in drawButtons)
                {
                    drawBtn.Init(OnDrawButtonClick);
                }

                float tweenerTime = 0f;
                UITweener lastTweener = null;
                foreach (var tweener in tweeners) {
                    if (tweener != null)
                    {
                        tweener.ResetToBeginning();
                        tweener.PlayForward();
                        if (tweenerTime <= tweener.duration + tweener.delay)
                        {
                            tweenerTime = tweener.duration + tweener.delay;
                            lastTweener = tweener;
                        }
                    }
                }
                lastTweener.SetOnFinished(delegate {
                    totalMask.SetActive(false);
                    foreach (var drawBtn in drawButtons) {
                        drawBtn.Holde();
                    }
                });

                mask.GetComponent<BoxCollider>().enabled = false;
                tipLabel.gameObject.SetActive(false);
            }
            else {
                expBar.Over();
                addCoinLabel.GetComponent<TweenNumText>().Over();
                addExpLabel.GetComponent<TweenNumText>().Over();
                addPrestigeLabel.GetComponent<TweenNumText>().Over();

                loseAttackerHeadIcon.textureName = dh0.iconName;
                loseDefenderHeadIcon.textureName = dh1.iconName;
                loseAttackerHeadIcon.texture.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                loseAttackerHeadIcon.texture.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                loseAttackerNameLabel.text = "[2f2f2f]" + StaticData.lastAttackerRankInfo.nickName + "[-]";
                loseDefenderNameLabel.text = StaticData.lastDefenderRankInfo.nickName;
                tipLabel.gameObject.SetActive(true);
            }
            if (StaticData.myPVPRankInfo.rank == pvpRank)
            {
                rankChangeNoneObj.SetActive(true);
                rankChangeObj.SetActive(false);
            }
            else {
                rankChangeNoneObj.SetActive(false);
                rankChangeObj.SetActive(true);

                rankFromLabel.text = StaticData.myPVPRankInfo.rank.ToString();
                rankToLabel.text = pvpRank.ToString();
            }


            isDrawButtonClicked = false;
        }




        public override void AddBalanceReward()
        {

            if (GuideManager.Instance != null && GuideManager.Instance.GetRunningGuide().Equals("guide_pvp"))
            {
                //var msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResultGuide;
                //if (msg != null)
                //{
                //    StaticData.playerData.AddResultObj(msg.result);
                //}
            }
            else
            {
                //var msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResult;
                //if (msg != null)
                //{
                //    StaticData.playerData.AddResultObj(msg.result);
                //}
            }
            
        }

        bool isDrawButtonClicked = false;

        public void OnDrawButtonClick(GameObject obj) {

            if (GuideManager.Instance != null && GuideManager.Instance.GetRunningGuide().Equals("guide_pvp"))
            {
                var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCPvpFightResultGuide;
                var clickBtn = obj.GetComponent<DrawButton>();
                clickBtn.PlayTurnEffect();
                clickBtn.SetIcon(msg.boxList[0], true);
                int index = 0;
                foreach (var btn in drawButtons)
                {
                    if (btn == clickBtn)
                        continue;
                    index++;
                    if (index == 1)
                        btn.SetIcon(msg.boxList[1], false, 0.5f);
                    else
                        btn.SetIcon(msg.boxList[2], false, 0.5f);
                }
                mask.GetComponent<BoxCollider>().enabled = true;
                tipLabel.gameObject.SetActive(true);
            }
            else
            {
                if (isDrawButtonClicked) {
                    return;
                }
                var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCPvpFightResult;
                var clickBtn = obj.GetComponent<DrawButton>();
                clickBtn.PlayTurnEffect();
                clickBtn.SetIcon(msg.boxList[0], true);
                int index = 0;
                foreach (var btn in drawButtons)
                {
                    if (btn == clickBtn)
                        continue;
                    index++;
                    if (index == 1)
                        btn.SetIcon(msg.boxList[1], false, 0.5f);
                    else
                        btn.SetIcon(msg.boxList[2], false, 0.5f);
                }
                mask.GetComponent<BoxCollider>().enabled = true;
                tipLabel.gameObject.SetActive(true);
                isDrawButtonClicked = true;
            }
        }


        public override void Exit()
        {
            base.Exit();
            Hide();
            ManagerController.Instance.BattleToRoom();
            Destroy(gameObject);
        }

        public override void OnTotalMaskClick()
        {
            base.OnTotalMaskClick();
            StopAllCoroutines();
            addCoinLabel.GetComponent<TweenNumText>().Over();
            addExpLabel.GetComponent<TweenNumText>().Over();
            addPrestigeLabel.GetComponent<TweenNumText>().Over();
            foreach (var drawBtn in drawButtons)
            {
                drawBtn.Holde();
            }
            expBar.Over();
        }

    }
}
